package manager
{
	import avmplus.getQualifiedClassName;
	
	import flash.utils.Dictionary;
	import flash.utils.getDefinitionByName;
	
	
	public class ObjectPool
	{
		private static var objDic:Dictionary = new Dictionary(true);
		
		public function ObjectPool()
		{
		}
		
		public static function getObj(cls:Class):Object{
			var obj:Object;
			if(objDic[cls]&&objDic[cls].length){
				obj = objDic[cls].shift();
			}else if(objDic[cls]&&!objDic[cls].length){	
				obj = new cls();
			}else{
				objDic[cls] = [];
				obj = new cls();
			}
		
			return obj;
		}
		
		public static function putObj(obj:Object):void{
			var cls:Class = getDefinitionByName(getQualifiedClassName(obj)) as Class;
			if(objDic[cls]){
				objDic[cls].push(obj);
			}
		}
		
		public static function destroyOneType(cls:Class):void{
			if(objDic[cls]){
				objDic[cls] = null;
				delete objDic[cls];
			}
		}
	}
}